package com.mall.group_booking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.constant.strings.MapMsg;
import com.mall.entity.TbItem;
import com.mall.entity.TbItmeSpec;
import com.mall.entity.bargain.BgBargainItem;
import com.mall.entity.groupbooking.GbActivity;
import com.mall.entity.groupbooking.GbItem;
import com.mall.group_booking.feign.ItemFeignService;
import com.mall.group_booking.feign.ItemSpecFeignService;
import com.mall.group_booking.mapper.BookingActivityMapper;
import com.mall.group_booking.mapper.BookingItemMapper;
import com.mall.group_booking.service.BookingActivityService;
import com.mall.utils.IdWorker;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author White
 * @version 1.0
 * @email 616769721@qq.com
 * @date 2021/7/30 8:46
 */
@Service
@Transactional
public class BookingActivityServiceImpl implements BookingActivityService {

    @Autowired
    private BookingActivityMapper bookingActivityMapper;

    @Autowired
    private Redisson redisson;

    @Autowired
    private ItemFeignService itemFeignService;

    @Autowired
    private ItemSpecFeignService itemSpecFeignService;

    @Autowired
    private BookingItemMapper itemMapper;

    @Autowired
    private IdWorker idWorker;


    @Override
    public void addActivityToRedis() {
        List<GbActivity> gbItemList=bookingActivityMapper.findByTime("2021-07-28 20:10:00");
        Map<Long, GbActivity> maps = gbItemList.stream().collect(Collectors
                .toMap(GbActivity::getId, Function.identity(), (key1, key2) -> key2));
        RMap<Long, GbActivity> itemMap = redisson.getMap(MapMsg.BOOKING_ACTIVITY_REDIS_MAP);
        RLock lock = redisson.getLock(MapMsg.LOCK_BOOKING_ACTIVITY_REDIS_MAP);
        try {
            lock.lock(5, TimeUnit.SECONDS);
            itemMap.clear();
            itemMap.putAll(maps);
        }finally {
            lock.unlock();
        }
    }

    /**
     * 添加活动
     *
     * @param activity 活动
     * @return boolean
     */
    @Override
    public void addActivity(GbActivity activity) {
        Integer count = 0;
        // 商品多规格
        if (activity.getActivityItemList().size() >= 1) {
            for (GbItem gbItem : activity.getActivityItemList()) {
                gbItem.setSpecItemId(gbItem.getId());
                TbItem item = itemFeignService.findByGoodsId(gbItem.getGoodsId());
                gbItem.setItemId(item.getId());
                gbItem.setLimitSurplus(gbItem.getLimitNum());
                itemMapper.add(gbItem);
                // 减少库存
                TbItmeSpec itemSpec = itemSpecFeignService.findById(gbItem.getSpecItemId());
                itemSpec.setInventory(itemSpec.getInventory() - gbItem.getLimitNum());
                itemSpecFeignService.update(itemSpec);
                count += gbItem.getLimitNum();
                item.setInventory(item.getInventory() - gbItem.getLimitNum());
                itemFeignService.update(item);
            }
            activity.setPrice(activity.getActivityItemList().stream().min(Comparator.comparing(GbItem::getGbPrice)).get().getGbPrice());
            activity.setLimitNum(count);
        } else {
            // 单规格商品
            GbItem gbItem = new GbItem();
            gbItem.setSpecItemId(gbItem.getItemId());
            gbItem.setItemId(activity.getItemId());
            gbItem.setGbPrice(activity.getPrice());
            gbItem.setLimitNum(activity.getLimitNum());
            gbItem.setLimitSurplus(activity.getLimitNum());
            itemMapper.add(gbItem);
            TbItem one = itemFeignService.findOne(gbItem.getItemId());
            one.setInventory(one.getInventory() - gbItem.getLimitNum());
            itemFeignService.update(one);
        }
        activity.setId(idWorker.nextId());
        // 限量剩余
        activity.setLimitSurplus(activity.getLimitNum());
        activity.setPartIn(0);
        activity.setSuccessCount(0);
        activity.setPayLimit(0);
        activity.setStartTime(new Date());
        bookingActivityMapper.addActivity(activity);
    }

    @Override
    public PageInfo<GbActivity> findPage(GbActivity activity, int page, int size) {
        PageHelper.startPage(page, size);
        List<GbActivity> list = bookingActivityMapper.findPage(activity);
        PageInfo<GbActivity> gbActivityPageInfo = new PageInfo<>(list);
        return gbActivityPageInfo;
    }

    @Override
    public void updateStatusById(Long id, String status) {
        bookingActivityMapper.updateStatusById(id, status);
    }

    @Override
    public void delActivityOne(Long id) {
        bookingActivityMapper.updateStatusById(id,"2");
    }

}
